/**
 * 
 */
package com.furschina.struts2.settings;

import java.awt.Image;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;

import cn.wooshai.core.util.ImageUtil;

import com.furschina.Constants;
import com.furschina.model.User;
import com.furschina.service.UserService;
import com.furschina.struts2.BaseAction;

/**
 * @author iday
 * 
 */
@Controller
@InterceptorRefs({ @InterceptorRef(Constants.DEFAULT_STACK_REF),
		@InterceptorRef(Constants.VALIDATOR_REF) })
public class AccountAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6025173732853894979L;

	@Valid
	private User user;
	private UserService userService;
	private String id;

	private File avatar;
	private String avatarFileName;

	@Value("${data.dir.path}")
	private String dataPath;

	private int aleft;
	private int atop;
	private int width;
	private int height;

	/**
	 * @param avatarPath
	 *            the avatarPath to set
	 */
	public void setDataPath(String dataPath) {
		this.dataPath = dataPath;
	}

	/**
	 * @return the avatar
	 */
	public File getAvatar() {
		return avatar;
	}

	/**
	 * @param avatar
	 *            the avatar to set
	 */
	public void setAvatar(File avatar) {
		this.avatar = avatar;
	}

	/**
	 * @return the avatarFileName
	 */
	public String getAvatarFileName() {
		return avatarFileName;
	}

	/**
	 * @param avatarFileName
	 *            the avatarFileName to set
	 */
	public void setAvatarFileName(String avatarFileName) {
		this.avatarFileName = avatarFileName;
	}

	public int getAleft() {
		return aleft;
	}

	public void setAleft(int aleft) {
		this.aleft = aleft;
	}

	public int getAtop() {
		return atop;
	}

	public void setAtop(int atop) {
		this.atop = atop;
	}

	/**
	 * @return the width
	 */
	public int getWidth() {
		return width;
	}

	/**
	 * @param width
	 *            the width to set
	 */
	public void setWidth(int width) {
		this.width = width;
	}

	/**
	 * @return the height
	 */
	public int getHeight() {
		return height;
	}

	/**
	 * @param height
	 *            the height to set
	 */
	public void setHeight(int height) {
		this.height = height;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the model
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param model
	 *            the model to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @param userService
	 *            the userService to set
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String index() {
		this.user = this.getLoginUser().getUser();
		return INDEX;
	}

	public String create() {
		User u = this.getLoginUser().getUser();
		if (!StringUtils.equals(this.user.getEmail(), u.getEmail())) {
			return ERROR;
		}
		if (StringUtils.isNotEmpty(avatarFileName)) {
			try {
				Image img = ImageUtil.crop(ImageIO.read(avatar), aleft, atop,
						width, height);
				img = ImageUtil.scale(img, Constants.AVATAR_SIZE,
						Constants.AVATAR_SIZE);
				String filename = UUID.randomUUID().toString() + ".png";
				File file = new File(dataPath + File.separator
						+ Constants.AVATAR_PATH + filename);
				ImageIO.write((RenderedImage) img, "png", file);
				ImageUtil.scale(file, Constants.AVATAR_MEDIUM_SIZE,
						Constants.AVATAR_MEDIUM_SIZE, true);
				ImageUtil.scale(file, Constants.AVATAR_SMALL_SIZE,
						Constants.AVATAR_SMALL_SIZE, true);
				this.user.setAvatar(filename);
				this.getLoginUser().getUser().setAvatar(filename);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		this.userService.modifyself(this.user);
		return SUCCESS;
	}

	public String val() {
		return null;
	}

}
